package cn.com.thingo.shyintelligent.presenter;

import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZAccessToken;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.List;

import javax.inject.Inject;

import cn.com.thingo.shyintelligent.base.RxPresenter;
import cn.com.thingo.shyintelligent.base.contract.MonitorContract;
import cn.com.thingo.shyintelligent.model.DataManager;
import cn.com.thingo.shyintelligent.model.bean.CommonBean;
import cn.com.thingo.shyintelligent.util.LogUtil;
import cn.com.thingo.shyintelligent.util.RxUtil;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MonitorPresenter extends RxPresenter<MonitorContract.View> implements MonitorContract.Presenter {
    private DataManager dataManager;

    @Inject
    public MonitorPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void messageCount(String token) {
        addSubscribe(dataManager.messageCount(token)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getCode() == 0) {
                            mView.viewMessageCount(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void getDeviceInfoList() {
        Observable.create(new ObservableOnSubscribe<List<EZDeviceInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<EZDeviceInfo>> e) throws Exception {
                EZAccessToken ezAccessToken = EZOpenSDK.getInstance().getEZAccessToken();
                List<EZDeviceInfo> infoList = null;
                if (ezAccessToken != null) {
                    try {
                        infoList = EZOpenSDK.getInstance().getDeviceList(0, 20);
                    } catch (BaseException e1) {
                        e1.printStackTrace();
                    }
                }
                e.onNext(infoList);
            }
        }).subscribeOn(Schedulers.newThread()).observeOn(
                AndroidSchedulers.mainThread()
        ).subscribe(new Consumer<List<EZDeviceInfo>>() {
            @Override
            public void accept(List<EZDeviceInfo> deviceInfoList) throws Exception {
                mView.showDeviceInfoList(deviceInfoList);
            }

        });
    }

}
